﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace NetworkIndexer.CoreExtensions.ServiceLocation
{
    public class ServiceLocatorContext
    {
        private readonly Stack<string> _contextStack = new Stack<string>();
        public const string PlugIn = "PLUGIN";
        public const string Console = "CONSOLE";

        public ServiceLocatorContext()
        {
            this._contextStack.Push("CONSOLE");
        }

        public IDisposable EnterContext(string context)
        {
            this._contextStack.Push(context);
            return new ContextReset(this);
        }

        public string GetContext()
        {
            return this._contextStack.Peek();
        }

        public Stack<string> GetStack()
        {
            return this._contextStack;
        }

        private class ContextReset : IDisposable
        {
            private readonly ServiceLocatorContext _context;

            public ContextReset(ServiceLocatorContext context)
            {
                this._context = context;
            }

            public void Dispose()
            {
                this._context._contextStack.Pop();
            }
        }
    }
}
